/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.service;

import java.io.File;
import java.util.List;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;
import javacommon.util.Notes;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.tech15.core.model.Attach;
import com.tech15.core.service.AttachManager;
import com.tech15.pub.dao.PubSoftDao;
import com.tech15.pub.model.PubInfo;
import com.tech15.pub.model.PubSoft;
import com.tech15.pub.vo.query.PubSoftQuery;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class PubSoftManager extends BaseManager<PubSoft,java.lang.String>{

	private PubSoftDao pubSoftDao;
	private AttachManager attachManager;
	public void setAttachManager(AttachManager attachManager) {
		this.attachManager = attachManager;
	}
	public void setPubSoftDao(PubSoftDao dao) {
		this.pubSoftDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.pubSoftDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(PubSoftQuery query) {
		return pubSoftDao.findPage(query);
	}
	
	@Transactional(readOnly=true)
	public PubSoft getById(String id){
		PubSoft pubsoft = pubSoftDao.getById(id);
		List<Attach> attach = attachManager.findByTableNameAndId(Notes.TABLE_PUB_SOFT, ""+id);
		pubsoft.setAttach(attach);
		return pubsoft;
	}
	
	public void saveSoft(PubSoft pubSoft, String realpath, File attachFile, String attachFileFileName, String attachFileContentType) {
		pubSoftDao.save(pubSoft);
		pubSoftDao.flush();
		if(attachFile != null){
			attachManager.saveAttach(realpath, pubSoft.getUserid(), Notes.TABLE_PUB_SOFT, ""+pubSoft.getId(), attachFile, attachFileFileName, attachFileContentType);
		}
	}
	public void updateSoft(PubSoft pubSoft, String realpath, File attachFile, String attachFileFileName, String attachFileContentType) {
		pubSoftDao.update(pubSoft);
		if(attachFile != null){
			attachManager.delete(Notes.TABLE_PUB_SOFT, pubSoft.getId());
			attachManager.saveAttach(realpath, pubSoft.getUserid(), Notes.TABLE_PUB_SOFT, ""+pubSoft.getId(), attachFile, attachFileFileName, attachFileContentType);
		}
	}
	
}
